import json
import os

import appium
from PIL.TiffTags import DOUBLE
from appium.webdriver import webdriver
from  selenium import webdriver

from config import BASE_DIR, HOST
import logging.handlers

class GetDriver:
    #获取appdriver
    __app_driver = None
    #获取appdriver
    __web_driver = None

    @classmethod
    def get_app_driver(cls):
        if cls.__app_driver is None:
            desired_caps = {}
            desired_caps["platformName"] = "Android"
            desired_caps["platformVersion"] = "12"
            desired_caps["deviceName"] = "127.0.0.1:62001"
            desired_caps["appPackage"] = "cn.ischinese.zzh"
            desired_caps["appActivity"] = "cn.ischinese.zzh.home.SplashActivity"
            desired_caps["nicodeKeyboard"] = True
            desired_caps["resetKeyboard"] = True
            cls.__app_driver = appium.webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

        return cls.__app_driver

    #获取webdriver
    @classmethod
    def get_web_driver(cls):
        if cls.__web_driver is None:
            cls.driver = webdriver.Chrome()
            cls.driver.get(HOST)
            cls.driver.maximize_window()
        return  cls.driver


def read_json(filename,key):
    file_path = BASE_DIR +os.sep + "data" + os.sep +filename
    new_list = []
    with open(file_path,"r",encoding="utf-8") as f:
        for data in json.load(f).get(key):
            new_list.append(tuple(data.values())[1:])
            return new_list
#日志封装
class GetLog:
    __log = None
    @classmethod
    def get_log(cls):
        if cls.__log is None:
            #获取日志器
            cls.__log = logging.getLogger()
            #设置入口级别
            cls.__log.setLevel(logging.INFO)
            #获取处理器
            filename = BASE_DIR + os.sep +"log" +os.sep + "zzh_auto.log"
            tf = logging.handlers.TimedRotatingFileHandler(filename=filename,
                                                           when = "midnight",
                                                           interval=1,
                                                           backupCount=3,
                                                           encoding="utf-8")
            #获取格式器
            fmt = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
            fm = logging.Formatter(fmt)
            #将格式器添加到处理器
            tf.setFormatter(fm)
            #将处理器添加到日志器
            cls.__log.addHandler(tf)
        #返回日志器
        return  cls.__log

if __name__ == '__main__':
    print(read_json("app_order.json", "order"))
    GetLog.get_log().info("日志测试")
